package com.qdlc.p2p.biz.service.activity.programme;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import scala.annotation.bridge;

import com.qdlc.p2p.biz.service.AdditionalUserCardService;
import com.qdlc.p2p.biz.service.activity.AbstractActivityProgrammeHandler;
import com.qdlc.p2p.biz.service.activity.ActivityPlanException;
import com.qdlc.p2p.biz.service.activity.ActivityProgrammeEnum;
import com.qdlc.p2p.biz.service.activity.param.ActivityProgrammeUserCardParam;
import com.qdlc.p2p.biz.service.additional.CardRuleService;
import com.qdlc.p2p.common.constant.UserCardStatus;
import com.qdlc.p2p.common.enums.EnumCardType;
import com.qdlc.p2p.common.util.CollectionUtils;
import com.qdlc.p2p.common.util.DataUtils;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.dto.AdditionalUserCard;
import com.qdlc.p2p.dal.model.CardRuleModel;

/**
 * 功能描述: 用户卡片活动处理类
 * @author linya
 * @date 2016-5-9
 */
@Service
public class ActivityUserCardProgrammeHandler extends AbstractActivityProgrammeHandler<ActivityProgrammeUserCardParam>{

	@Resource
	CardRuleService cardRuleService;
	
	@Resource
	AdditionalUserCardService additionalUserCardService;
	
	@Override
	public ActivityProgrammeEnum supportsProgramme() {
		return ActivityProgrammeEnum.USER_CARD;
	}

	@Override
	public ActivityProgrammeUserCardParam createProgrammeParam(Object attatchmentObject) {
		if(attatchmentObject == null){
			return new ActivityProgrammeUserCardParam();
		}
		
		if(attatchmentObject instanceof ActivityProgrammeUserCardParam){
			return (ActivityProgrammeUserCardParam)attatchmentObject;
		}
		
		return new ActivityProgrammeUserCardParam();
	}

	@Override
	public void handleCustom(ActivityProgrammeUserCardParam param) throws ActivityPlanException {
		
		//查询所有可用的酒卡规则记录
		List<CardRuleModel> list = cardRuleService.findUsableCardRules();
		
		//获取一条cardType类型的酒卡记录
		CardRuleModel rule = null;
		if(CollectionUtils.isNotNullOrEmpty(list)){
			for(CardRuleModel item: list){
				if(item.getType() == param.getCardType().getType()){
					rule = item;
					break;
				}
			}
		}
		
		if(rule != null){
			//锁住行记录（解决并发）
			rule = cardRuleService.findCardRuleById(rule.getId());
			//发卡
			populationUserCard(param, rule);
		}
		
	}

	@Override
	protected void handleInternal(ActivityProgrammeUserCardParam param) throws ActivityPlanException {
		
		//卡类型
		EnumCardType cardType = param.getCardType();
		if(cardType == null){
			return;
		}
		
		//查看卡片规则
		long ruleId = param.getRuleId();
		//锁住行记录（解决并发）
		CardRuleModel model = cardRuleService.findCardRuleById(ruleId);
		
		//给用户送卡片
		populationUserCard(param, model);
		
	}

	private void populationUserCard(ActivityProgrammeUserCardParam param, CardRuleModel model){
		
		int total = model.getTotal();
		//判断是否已经发送完
		if(total == 0){
			return;
		}
		
		//构建用户卡记录
		AdditionalUserCard userCard = new AdditionalUserCard();
		Date nowDate = DateUtil.getNow();
		int validDay = model.getValidDay();
		userCard.setUserId(param.getUser().getUserId());
		userCard.setAddTime(nowDate);
		userCard.setName(param.getCardType().getName());
		userCard.setSourceType(param.getActivityPlan().getCnTache());
		userCard.setStatus(UserCardStatus.STATUS_NO_USE);
		userCard.setType(param.getCardType().getType());
		userCard.setVersion(1L);
		userCard.setExpiredTime(DateUtil.rollDay(nowDate, validDay));
		userCard.setValidDay(validDay);
		userCard.setKeyId(model.getId());
		//保存用户卡
		additionalUserCardService.save(userCard);
		
		//扣除卡片规则中的使用数量
		model.setTotal(total - 1);
		model.setIssueNum(model.getIssueNum() + 1);
		cardRuleService.updateCardRule(model);
	}
	
}
